#include "stdafx.h"
#include "SettingsDef.h"
#include "Util.h"

CIniValue* CIniValueFactory::Get(EIniValueType type, const TCHAR* val)
{
  CIniValue* pVal;
  switch(type)
  {
  case ivString:
    pVal = new CIniStringValue(val);
    break;
  case ivInt:
    pVal = new CIniIntValue(val);
    break;
  case ivBool:
    pVal = new CIniBoolValue(val);
    break;
  case ivColor:
    pVal = new CIniColorValue(val);
    break;
  case ivGrad:
    pVal = new CIniGradValue(val);
    break;
  case ivFont:
    pVal = new CIniFontValue(val);
    break;
  case ivPen:
    pVal = new CIniPenValue(val);
    break;
  default:  // unsupported
    pVal = NULL;
    break;
  }
  return pVal;
}

void CIniColorValue::Set(const TCHAR* val)
{
  CUtil::ParseColor(val, m_val);
}

void CIniGradValue::Set(const TCHAR* val)
{
  CString str = val;
  int curPos = 0;
  CString token = str.Tokenize(TEXT(","), curPos);
  if (!token.IsEmpty())
  {
    token.Trim();
    CUtil::ParseColor(token.GetString(), m_val1);

    token = str.Tokenize(TEXT(","), curPos);
    if (!token.IsEmpty())
    {
      token.Trim();
      CUtil::ParseColor(token.GetString(), m_val2);
    }
  }
}

void CIniFontValue::Set(const TCHAR* val)
{
  if (!val)
    return;

  CString str = val;
  int curPos = 0;
  CString token = str.Tokenize(TEXT(","), curPos);
  if (!token.IsEmpty())
  {
    token.Trim();
    m_name = token;

    token = str.Tokenize(TEXT(","), curPos);
    if (!token.IsEmpty())
    {
      token.Trim();
      m_size = ::_ttoi(token.GetString());

      token = str.Tokenize(TEXT(","), curPos);
      if (!token.IsEmpty())
      {
        token.Trim();
        m_bold = token.CompareNoCase(TEXT("bold")) == 0;

        token = str.Tokenize(TEXT(","), curPos);
        if (!token.IsEmpty())
        {
          token.Trim();
          m_italic = token.CompareNoCase(TEXT("italic")) == 0;
        }     
      }     
    }
  }
}

void CIniPenValue::Set(const TCHAR* val)
{
  if (!val)
    return;

  CString str = val;
  int curPos = 0;
  CString token = str.Tokenize(TEXT(","), curPos);
  if (!token.IsEmpty())
  {
    token.Trim();
    CUtil::ParseColor(token.GetString(), m_color);

    token = str.Tokenize(TEXT(","), curPos);
    if (!token.IsEmpty())
    {
      token.Trim();
      if (token.CompareNoCase(TEXT("dash")) == 0)
        m_style = PS_DASH;
      else if (token.CompareNoCase(TEXT("null")) == 0)
        m_style = PS_NULL;
      else
        m_style = PS_SOLID;

      token = str.Tokenize(TEXT(","), curPos);
      if (!token.IsEmpty())
      {
        token.Trim();
        m_width = ::_ttoi(token.GetString());
      }
    }
  }
}